package com.b2c.oa.controller;

import com.ims.entity.MediaAccountEntity;
import com.ims.entity.MediaDataReportItemEntity;
import com.ims.service.MediaAccountService;
import com.ims.service.MediaDataReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/media")
public class MediaDataInputController {
    @Autowired
    private MediaAccountService mediaAccountService;
    @Autowired
    private MediaDataReportService reportService;


    /**
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/data_input", method = RequestMethod.GET)
    public String login(Model model, HttpServletRequest request) {

        //查询客户列表
        List<MediaAccountEntity> mediaList = mediaAccountService.getList(null);
        model.addAttribute("mediaList", mediaList);

        model.addAttribute("menuId", "media_data_input");
        return "media_data_report_input";

    }

    @RequestMapping(value = "/data_input", method = RequestMethod.POST)
    public String postSystemOrder(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object userId = request.getSession().getAttribute("userId");

        Integer mediaAccountId = Integer.parseInt(request.getParameter("mediaAccountId"));
        Integer userCount = Integer.parseInt(request.getParameter("userCount"));


        /*******/
        String[] publishDate = request.getParameterValues("publishDate");
        String[] viewCount = request.getParameterValues("viewCount");
        String[] zanCount = request.getParameterValues("zanCount");
        String[] commentCount = request.getParameterValues("commentCount");

        List<MediaDataReportItemEntity> reportItemList = new ArrayList<>();
        for (int i=0;i<publishDate.length;i++) {
            if( StringUtils.isEmpty(publishDate[i])==false){
                MediaDataReportItemEntity item = new MediaDataReportItemEntity();
                item.setPublishDate(publishDate[i]);
                item.setViewCount(Integer.parseInt(viewCount[i]));
                item.setZanCount(Integer.parseInt(zanCount[i]));
                item.setCommentCount(Integer.parseInt(commentCount[i]));
                reportItemList.add(item);
            }

        }


        String summary  = request.getParameter("summary");
        String improvement = request.getParameter("improvement");
        String reportPeriod = request.getParameter("reportPeriod");

        reportService.addData(mediaAccountId,userCount,summary,improvement,reportPeriod,reportItemList);

        return "redirect:/media/data_report";
    }


    @RequestMapping(value = "/data_report_detail", method = RequestMethod.GET)
    public String detail(Model model, @RequestParam Integer id, HttpServletRequest request) {
        var report = reportService.getById(id);
        model.addAttribute("report", report);

        model.addAttribute("menuId", "media_data_report");
        return "media_data_report_detail";

    }

    @RequestMapping(value = "/data_report_detail", method = RequestMethod.POST)
    public String dataReportUpdate(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object userId = request.getSession().getAttribute("userId");

        Integer id = Integer.parseInt(request.getParameter("id"));
        Integer userCount = Integer.parseInt(request.getParameter("userCount"));


        /*******/
        String[] itemId = request.getParameterValues("itemId");
        String[] publishDate = request.getParameterValues("publishDate");
        String[] viewCount = request.getParameterValues("viewCount");
        String[] zanCount = request.getParameterValues("zanCount");
        String[] commentCount = request.getParameterValues("commentCount");

        List<MediaDataReportItemEntity> reportItemList = new ArrayList<>();
        for (int i=0;i<itemId.length;i++) {
            if( StringUtils.isEmpty(itemId[i])==false){

                MediaDataReportItemEntity item = new MediaDataReportItemEntity();
                item.setId(Integer.parseInt(itemId[i]));
                item.setPublishDate(publishDate[i]);
                item.setViewCount(Integer.parseInt(viewCount[i]));
                item.setZanCount(Integer.parseInt(zanCount[i]));
                item.setCommentCount(Integer.parseInt(commentCount[i]));
                reportItemList.add(item);
            }

        }


        String summary  = request.getParameter("summary");
        String improvement = request.getParameter("improvement");
        String reportPeriod = request.getParameter("reportPeriod");

        reportService.editData(id,userCount,summary,improvement,reportPeriod,reportItemList);

        return "redirect:/media/data_report";
    }

    /**
     * 运营数据录入
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/data_input_m", method = RequestMethod.GET)
    public String data_input_m(Model model, HttpServletRequest request) {

        //查询客户列表
        List<MediaAccountEntity> mediaList = mediaAccountService.getList(null);
        model.addAttribute("mediaList", mediaList);

        model.addAttribute("menuId", "media_data_input");
        return "media_data_report_input_m";

    }
}
